using System;
using System.Drawing;
using System.ComponentModel;
using System.Collections.Generic;

namespace SCADAEditor.Component
{
    public class DateTimeComponent : ComponentBase
    {
        private bool _showDate = true;
        private DateFormat _dateFormat = DateFormat.YYYYMMDD;
        private bool _showWeek = true;
        private bool _showTime = true;
        private TimeFormat _timeFormat = TimeFormat.HHMMSS;
        private Font _font = new Font("宋体", 12);
        private Color _fontColor = Color.Black;
        
        [Category("日期设置")]
        [Description("是否显示日期")]
        public bool ShowDate
        {
            get { return _showDate; }
            set { _showDate = value; OnPropertyChanged(); }
        }
        
        [Category("日期设置")]
        [Description("日期显示格式")]
        public DateFormat DateFormat
        {
            get { return _dateFormat; }
            set { _dateFormat = value; OnPropertyChanged(); }
        }
        
        [Category("日期设置")]
        [Description("是否显示星期")]
        public bool ShowWeek
        {
            get { return _showWeek; }
            set { _showWeek = value; OnPropertyChanged(); }
        }
        
        [Category("时间设置")]
        [Description("是否显示时间")]
        public bool ShowTime
        {
            get { return _showTime; }
            set { _showTime = value; OnPropertyChanged(); }
        }
        
        [Category("时间设置")]
        [Description("时间显示格式")]
        public TimeFormat TimeFormat
        {
            get { return _timeFormat; }
            set { _timeFormat = value; OnPropertyChanged(); }
        }
        
        [Category("外观")]
        [Description("文本字体")]
        public Font Font
        {
            get { return _font; }
            set { _font = value; OnPropertyChanged(); }
        }
        
        [Category("外观")]
        [Description("文本颜色")]
        public Color FontColor
        {
            get { return _fontColor; }
            set { _fontColor = value; OnPropertyChanged(); }
        }

        public DateTimeComponent()
        {
            Size = new Size(123, 23);
            Text = ""; // 设置默认文本
            IsEditMode = true;
        }

        public override void Draw(Graphics g)
        {
            string displayText = GetDisplayText();
            using (var brush = new SolidBrush(_fontColor))
            {
                g.DrawString(displayText, _font, brush, Position);
            }
        }

        private string GetDisplayText()
        {
            DateTime now = DateTime.Now;
            string text = "";
            
            if (_showDate)
            {
                text += _dateFormat == DateFormat.YYYYMMDD ? 
                    now.ToString("yyyy/MM/dd") : now.ToString("MM/dd/yy");
                
                if (_showWeek)
                {
                    text += " " + GetChineseWeekDay(now.DayOfWeek);
                }
            }
            
            if (_showTime)
            {
                if (!string.IsNullOrEmpty(text)) text += " ";
                text += _timeFormat == TimeFormat.HHMMSS ? 
                    now.ToString("HH:mm:ss") : now.ToString("HH:mm");
            }
            
            return text;
        }

        private string GetChineseWeekDay(DayOfWeek day)
        {
             switch (day)
            {
                case DayOfWeek.Sunday:
                    return "星期日";
                case DayOfWeek.Monday:
                    return "星期一";
                case DayOfWeek.Tuesday:
                    return "星期二";
                case DayOfWeek.Wednesday:
                    return "星期三";
                case DayOfWeek.Thursday:
                    return "星期四";
                case DayOfWeek.Friday:
                    return "星期五";
                case DayOfWeek.Saturday:
                    return "星期六";
                default:
                    return "";
            }
        }

        public override Dictionary<string, object> ToJson()
        {
            var dict = base.ToJson();
            dict.Add("ShowDate", _showDate);
            dict.Add("DateFormat", (int)_dateFormat);
            dict.Add("ShowWeek", _showWeek);
            dict.Add("ShowTime", _showTime);
            dict.Add("TimeFormat", (int)_timeFormat);
            dict.Add("Font", _font.Name);
            dict.Add("FontSize", _font.Size);
            dict.Add("FontColor", _fontColor.ToArgb());
            return dict;
        }

        public override void FromJson(Dictionary<string, object> json)
        {
            base.FromJson(json);
            _showDate = (bool)json["ShowDate"];
            _dateFormat = (DateFormat)(int)json["DateFormat"];
            _showWeek = (bool)json["ShowWeek"];
            _showTime = (bool)json["ShowTime"];
            _timeFormat = (TimeFormat)(int)json["TimeFormat"];
            var fontName = (string)json["Font"];
            var fontSize = Convert.ToSingle(json["FontSize"]);
            _font = new Font(fontName, fontSize);
            _fontColor = Color.FromArgb((int)json["FontColor"]);
        }
    }

    public enum DateFormat
    {
        YYYYMMDD,
        MMDDYY
    }

    public enum TimeFormat
    {
        HHMMSS,
        HHMM
    }
}